package com.cgwx.tmservice.netty;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cgwx.tmservice.config.CanPortConfig;
import com.cgwx.tmservice.config.SPortConfig;
import com.cgwx.tmservice.entity.bean.TmFrameConfigIdToIdentified;
import com.cgwx.tmservice.entity.bean.TmFrameInfoBean;
import com.cgwx.tmservice.entity.bean.TmParamConfigIdToTmCode;
import com.cgwx.tmservice.entity.bean.TmTableStatic;
import com.cgwx.tmservice.entity.tm.TmFrameConfig;
import com.cgwx.tmservice.entity.tm.TmParametersConfig;
import com.cgwx.tmservice.service.TmFrameConfigService;
import com.cgwx.tmservice.service.TmInfoDateTableService;
import com.cgwx.tmservice.service.TmParametersConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author GaoWei
 * @Date 2023/8/30 - 11:01
 */
@Component
public class UdpReceiveInitApplication implements ApplicationRunner {

    @Autowired
    private CanPortConfig canPortConfig;
    @Autowired
    private SPortConfig sPortConfig;
    @Autowired
    private NettyCanUdpReceiveServer nettyCanUdpReceiveServer;
    @Autowired
    private NettySUdpReceiveServer nettySUdpReceiveServer;
    @Autowired
    private TmInfoDateTableService tmInfoDateTableService;
    @Autowired
    private TmFrameConfigService tmFrameConfigService;
    @Autowired
    private TmParametersConfigService tmParametersConfigService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        createTable();
        tmConfigInit();
        nettyCanUdpReceiveServer.init(canPortConfig.getListeningPort());
        nettySUdpReceiveServer.init(sPortConfig.getListeningPort());
    }

    private void createTable(){
        SimpleDateFormat formatTableNameDate = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String tmInfoLogDateTableName = "tm_info_log_" + formatTableNameDate.format(date);
        String tmInfoHisDateTableName = "tm_info_his_" + formatTableNameDate.format(date);
        tmInfoDateTableService.createTmInfoLogDateTable(tmInfoLogDateTableName);
//        记录下当天的遥测原码表的名称
        TmTableStatic.TM_LOG_TABLE_NAME = tmInfoLogDateTableName;
        tmInfoDateTableService.createTmInfoHisDateTable(tmInfoHisDateTableName);
//        记录下当天的遥测解析表的名称
        TmTableStatic.TM_HIS_TABLE_NAME = tmInfoHisDateTableName;
    }

    public void tmConfigInit(){
//        查询遥测帧配置表中的全部遥测帧识别码，封装到allTmFrameIdentifiersList中
        QueryWrapper<TmFrameConfig> queryWrapperTmFrame = new QueryWrapper<>();
        queryWrapperTmFrame.select("tmframe_identifiers");
        List<TmFrameConfig> queryAllTmFrameIdentifiersList = tmFrameConfigService.list(queryWrapperTmFrame);
        List<String> allTmFrameIdentifiersList = new ArrayList<>();
        for (TmFrameConfig tmFrameConfig : queryAllTmFrameIdentifiersList) {
            allTmFrameIdentifiersList.add(tmFrameConfig.getTmframeIdentifiers());
        }
//        遍历遥测帧识别码，遍历遥测参数配置表，将此帧下的全部遥测参数配置存储到ArrayList中
        for (String tmFrameIdentifier : allTmFrameIdentifiersList) {
            QueryWrapper<TmParametersConfig> queryWrapperTmParameter = new QueryWrapper<>();
            queryWrapperTmParameter.eq("tmframe_identifiers",tmFrameIdentifier);
            List<TmParametersConfig> list = tmParametersConfigService.list(queryWrapperTmParameter);
//            创建集合，元素为TmParametersConfig；该集合作为Map的value；key为tmFrameIdentifier
            ArrayList<TmParametersConfig> tmParametersConfigList = new ArrayList<>(list);
            TmFrameInfoBean.TM_PARAMETER_INFO_MAP.put(tmFrameIdentifier,tmParametersConfigList);
        }

//        遍历所有遥测帧配置，得到id为key identifiers为value，存储
        List<TmFrameConfig> tmFrameConfigList = tmFrameConfigService.list();
        for (TmFrameConfig tmFrameConfig : tmFrameConfigList) {
            TmFrameConfigIdToIdentified.TM_FRAME_ID_IDENTIFIED.put(tmFrameConfig.getId(),tmFrameConfig.getTmframeIdentifiers());
        }

//        遍历所有遥测参数配置，得到id为key tmCode为value，存储
        List<TmParametersConfig> tmParametersConfigList = tmParametersConfigService.list();
        for (TmParametersConfig tmParametersConfig : tmParametersConfigList) {
            TmParamConfigIdToTmCode.TM_PARAM_ID_TMCODE.put(tmParametersConfig.getId(),tmParametersConfig.getTmCode());
        }
    }
}
